<?php

$getReq = "";
if ($data['method'] == "POST") {
    $reqParams = "reqParams {$data['function_name']}Req";
    $function  = "WithStruct(reqParams)";
    $commlib   = "";
} else {
    $reqParams = "reqParams {$data['function_name']}Req";
    $function  = "WithMultiQueryParam(commlib.QueryMap(reqParams))";
    $commlib   = '"git.inke.cn/bpc/server/sdk/commlib"';
}

$getReq = <<<EOF
type {$data['function_name']}Req struct {
	Uid int64 `json:"uid" schema:"uid"`
}
EOF;

$code = <<<EOF
package sdk

import (
	"context"
	"encoding/json"
	"fmt"
	"git.inke.cn/BackendPlatform/golang/logging"
	{$commlib}
	httpclient "git.inke.cn/inkelogic/daenerys/http/client"
)
const (
    {$data['uri_const']} = "{$data['uri']}"
)

type {$data['function_name']}Response struct {
	DMErr    int    `json:"dm_error"`
	ErrorMsg string `json:"error_msg"`
	Data     {$data['function_name']}Data `json:"data"`
}

type {$data['function_name']}Data struct {
    
}

{$getReq}

/**
 * @Description: {$data['function_desc']}
 * @param ctx
 * @param reqParams
 * @return resp
 * @return err
 */
func {$data['function_name']}(ctx context.Context, {$reqParams}) (resp {$data['function_name']}Response, err error) {
	return Default.{$data['function_name']}(ctx, reqParams)
}

/**
 * @Description: http请求，调用接口
 * @param ctx
 * @param reqParams
 * @return resp
 * @return err
 */
func (c *Client) {$data['function_name']}(ctx context.Context, {$reqParams}) (resp {$data['function_name']}Response, err error) {
    logger := logging.For(ctx, "func", "{$data['function_name']}", "reqParams", reqParams)
	req := httpclient.NewRequest(ctx).
		WithPath({$data['uri_const']}).
		WithMethod("{$data['method']}").
		{$function}

	response, err := c.client.Call(req)
	if err != nil {
	    logger.Errorf("request error,err:%v", err)
		return resp, err
	}

	if err := response.JSON(&resp); err != nil {
		logger.Errorf("json transfer error ,err :%v", err)
		return resp, err
	}

	if resp.DMErr != 0 {
	    logger.Errorf("{$data['function_name']} resp.DMErr != 0 ,err :%v", resp.ErrorMsg)
		return resp, fmt.Errorf("{$data['function_name']} error: %s", resp.ErrorMsg)
	}
	rspJson, _ := json.Marshal(resp)
	logger.Debugf("{$data['function_name']} uri:%v，param:%v, response result,resp:%+v，,X-Trace-Id=%v",{$data['uri_const']}, commlib.StructToJson(reqParams),string(rspJson), response.GetHeader("X-Trace-Id"))
	return resp, nil
}
EOF;
?>
<?= $code ?>
